﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace ModuleDuoc.DM
{
    public partial class frmTheKho_New : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        string sql;

        public frmTheKho_New()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;
        }
        public frmTheKho_New(string mavach)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;
            InitializeComponent();
            textBox1.Text = mavach.Trim();
            if (textBox1.Text == "")
            {
                textBox1.Text = "0";
            }
            getXuatNhapByMaVach(textBox1.Text);
        }
        private void setTab(Control c, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                c.Focus();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            dataGridView2.DataSource = null;
            getXuatNhapByMaVach(textBox1.Text.Trim());
        }

        private void getXuatNhapByMaVach(string ma)
        {
            sql = "SELECT zddt.TENVT, zddd.Ten, zdp.solo, zdp.mavach, zdp.soluong, SUM(zdp2.soluong) AS Xuat " +
                            "FROM zD_PHIEUNHAPKHOCT zdp " +
                            "INNER JOIN zD_PHIEUXUATKHOCT zdp2 ON zdp2.mavach = zdp.mavach " +
                            "INNER JOIN zD_DM_THUOC zddt ON zddt.mavt = zdp2.mavt " +
                            "INNER JOIN zD_DM_DVT zddd ON zddd.Ma = zddt.DVT " +
                            "WHERE zdp.mavach = " + ma + " " +
                            "GROUP BY zddt.TENVT, zddd.Ten, zdp.solo, zdp.mavach, zdp.soluong";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            getXuatByMaVach();
        }

        private void getXuatByMaVach()
        {
            sql = "SELECT zdp3.ngaylap, zdp2.sophieu, zddt.TENVT, zddd.Ten, zdp2.solo, zdp2.mavach, zdp2.soluong " +
                    "FROM zD_PHIEUXUATKHOCT zdp2 " +
                    "INNER JOIN zD_PHIEUXUATKHO zdp3 ON zdp3.sophieu = zdp2.sophieu " +
                    "INNER JOIN zD_DM_THUOC zddt ON zddt.mavt = zdp2.mavt " +
                    "INNER JOIN zD_DM_DVT zddd ON zddd.Ma = zddt.DVT " +
                    "WHERE zdp2.mavach = " + textBox1.Text.Trim() + "";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            dataGridView2.DataSource = dt;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1_Click(sender, e);
            }
        }

        private void frmTheKho_New_Load(object sender, EventArgs e)
        {

        }
    }
}
